/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import core.Change;
import core.ChangesLogger;
import core.Database;
import core.DatabaseTable;
import core.TableColumn;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import ui.helpers.DatabaseTree;
import ui.helpers.DrawPanel;
import ui.helpers.NewColumnListener;
import ui.helpers.SaveListener;
import ui.helpers.LoadListener;
import ui.helpers.ExportSqlListener;

import org.apache.log4j.Logger;

import java.io.File;
import java.math.BigDecimal;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JTree;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.JComboBox;



/**
 *
 * @author Sakis Vtdk <sakis.vtdk@gmail.com>
 */
public class ApplicationWindow extends javax.swing.JFrame {

	/**
	 * Creates new form ApplicationWindow
	 */
	private ApplicationWindow() {
		initComponents();
	}

	private ApplicationWindow(String title)
	{
		//if we are on a mac, we make use of the menu bar at the top of the screen
		if(DbVersioning.OS.indexOf("mac") != -1)
		{
			System.setProperty("apple.laf.useScreenMenuBar", "true");
		}

		initComponents();

		//set the listener for the closing event
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				if(Database.getDatabase().hasUnsavedChanges())
				{
					int choice = JOptionPane.showConfirmDialog(null, "There are some unsaved changes, do you want to save these changes?");
					if(choice == JOptionPane.YES_OPTION)
					{
						_instance.saveButton.doClick();
						_instance.dispose();
						System.exit(0);
					}
					else if(choice == JOptionPane.NO_OPTION)
					{
						_instance.dispose();
						System.exit(0);
					}
					else if(choice == JOptionPane.CANCEL_OPTION)
					{
						//do nothing
					}
				}
				else
				{
					_instance.dispose();
					System.exit(0);
				}
			}
		});

		jScrollPane1.setViewportView(_drawPanel);
		this.setTitle(title);
		Database.createNewDatabase(DEFAULT_NEW_DB_NAME);
		refreshItems();
	}

	public static ApplicationWindow getApplicationWindow(String title)
	{
		if(_instance == null)
		{
			_instance = new ApplicationWindow(title);
		}

		return _instance;
	}

	public static ApplicationWindow getApplicationWindow()
	{
		if(_instance == null)
		{
			_instance = new ApplicationWindow();
		}

		return _instance;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jToolBar1 = new javax.swing.JToolBar();
        saveButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        sqlButton = new javax.swing.JButton();
        sqlButton.addActionListener(new ExportSqlListener());
        jSeparator1 = new javax.swing.JToolBar.Separator();
        newTableButton = new javax.swing.JButton();
        newTableButton.addActionListener(new ui.helpers.NewTableListener());
        newColumnButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jLabel2 = new javax.swing.JLabel();
        versionTextField = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        versionsComboBox = new javax.swing.JComboBox();
        versionsComboBox.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {

                if(((JComboBox) e.getSource()).getItemCount() > 0)
                {
                    log.debug(e.getClass() +" | "+ e.getActionCommand() +" | "+ e.paramString());
                    //get the selected item
                    String selectedItem = (String) ((JComboBox) e.getSource()).getSelectedItem();

                    //get the version number
                    String version = selectedItem.substring(0, 3).trim();

                    if(ApplicationWindow.getApplicationWindow().getDisplayedVersion().compareTo(new BigDecimal(version)) != 0)
                    {
                        displayDatabase(new BigDecimal(version));

                        Database.restoreWorkingInstance();
                    }
                }
            }

        });
        jSplitPane1 = new javax.swing.JSplitPane();
        jSplitPane2 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        //set the tree so that only one node can be selected at the time
        jTree1.getSelectionModel().setSelectionMode(javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION);
        actionsScrollPane = new javax.swing.JScrollPane();
        jSplitPane3 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        changesTextArea = new javax.swing.JTextArea();
        jScrollPane5 = new javax.swing.JScrollPane();
        sqlTextArea = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newMenu = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        deleteMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        saveButton.setText("Save");
        saveButton.setFocusable(false);
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(new SaveListener());
        jToolBar1.add(saveButton);

        openButton.setText("Open");
        openButton.setFocusable(false);
        openButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        openButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        openButton.addActionListener(new LoadListener());
        jToolBar1.add(openButton);

        sqlButton.setText("Export Sql...");
        sqlButton.setFocusable(false);
        sqlButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        sqlButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(sqlButton);
        jToolBar1.add(jSeparator1);

        newTableButton.setText("newTbl");
        newTableButton.setFocusable(false);
        newTableButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newTableButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(newTableButton);

        newColumnButton.setText("newCol");
        newColumnButton.setEnabled(false);
        newColumnButton.setFocusable(false);
        newColumnButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newColumnButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newColumnButton.addActionListener(new NewColumnListener());
        jToolBar1.add(newColumnButton);
        jToolBar1.add(jSeparator2);

        jLabel2.setText("Creating Version");
        jToolBar1.add(jLabel2);

        versionTextField.setToolTipText("Base Version");
        versionTextField.setEnabled(false);
        versionTextField.setMaximumSize(new java.awt.Dimension(50, 20));
        versionTextField.setMinimumSize(new java.awt.Dimension(50, 20));
        versionTextField.setPreferredSize(new java.awt.Dimension(50, 20));
        jToolBar1.add(versionTextField);
        jToolBar1.add(jSeparator3);

        versionsComboBox.setMaximumSize(new java.awt.Dimension(200, 20));
        versionsComboBox.setMinimumSize(new java.awt.Dimension(200, 20));
        versionsComboBox.setPreferredSize(new java.awt.Dimension(200, 20));
        jToolBar1.add(versionsComboBox);

        jSplitPane1.setResizeWeight(0.13);
        jSplitPane1.setContinuousLayout(true);

        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jTree1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTree1);

        jSplitPane2.setLeftComponent(jScrollPane2);
        jSplitPane2.setRightComponent(actionsScrollPane);

        jSplitPane1.setLeftComponent(jSplitPane2);

        jSplitPane3.setDividerLocation(300);
        jSplitPane3.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane3.setContinuousLayout(true);
        jSplitPane3.setDoubleBuffered(true);

        jScrollPane1.setBackground(new java.awt.Color(51, 51, 255));
        jScrollPane1.setDoubleBuffered(true);
        jSplitPane3.setLeftComponent(jScrollPane1);

        changesTextArea.setEditable(false);
        changesTextArea.setColumns(20);
        changesTextArea.setRows(5);
        jScrollPane4.setViewportView(changesTextArea);

        jTabbedPane1.addTab("Changes", jScrollPane4);

        sqlTextArea.setEditable(false);
        sqlTextArea.setColumns(20);
        sqlTextArea.setRows(5);
        jScrollPane5.setViewportView(sqlTextArea);

        jTabbedPane1.addTab("Sql", jScrollPane5);

        jSplitPane3.setRightComponent(jTabbedPane1);

        jSplitPane1.setRightComponent(jSplitPane3);

        fileMenu.setMnemonic('f');
        fileMenu.setText("File");

        newMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenu.setText("New");
        newMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuActionPerformed(evt);
            }
        });
        fileMenu.add(newMenu);

        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem.setMnemonic('o');
        openMenuItem.setText("Open");
        openMenuItem.addActionListener(new LoadListener());
        fileMenu.add(openMenuItem);

        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveMenuItem.setMnemonic('s');
        saveMenuItem.setText("Save");
        saveMenuItem.addActionListener(new SaveListener());
        fileMenu.add(saveMenuItem);

        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setMnemonic('e');
        editMenu.setText("Edit");

        cutMenuItem.setMnemonic('t');
        cutMenuItem.setText("Cut");
        editMenu.add(cutMenuItem);

        copyMenuItem.setMnemonic('y');
        copyMenuItem.setText("Copy");
        editMenu.add(copyMenuItem);

        pasteMenuItem.setMnemonic('p');
        pasteMenuItem.setText("Paste");
        editMenu.add(pasteMenuItem);

        deleteMenuItem.setMnemonic('d');
        deleteMenuItem.setText("Delete");
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText("Help");

        contentsMenuItem.setMnemonic('c');
        contentsMenuItem.setText("Contents");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setMnemonic('a');
        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jToolBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1099, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jSplitPane1)
                .add(6, 6, 6))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
		this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void newMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuActionPerformed
		
		String dbName = JOptionPane.showInputDialog(null, "Enter a name for the database", "Create New Database", JOptionPane.PLAIN_MESSAGE);

		Database.createNewDatabase(dbName);
		
		actionsScrollPane.getViewport().add(new DatabaseActionsPanel());
		
		refreshItems();
    }//GEN-LAST:event_newMenuActionPerformed

	public void selectItemInJtree(String itemName)
	{
		//get the path of that component:
		TreePath path = DatabaseTree.findNode(itemName);

		if(path != null)
		{
			log.trace("Path is: "+ path.toString());

			//make it visible
			jTree1.setSelectionPath(path);
			jTree1.scrollPathToVisible(path);
		}
	}

	/**
	 * Loads the appropriate panel based on the selection of the tree
	 * @param evt 
	 */
    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        JTree tree = (JTree) evt.getSource();
		DefaultMutableTreeNode lastSelection = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

		if(lastSelection == null)
		{
			newColumnButton.setEnabled(false);
			return;
		}
		
		if(lastSelection.getLevel() == 2) //column
		{
			//get the table id
			TreeNode tableNode = lastSelection.getParent();
			int tableId = getSelectedTableId();

			//get the column id
			int columnId = tableNode.getIndex(lastSelection);

			//highlight the table
			_drawPanel.highlightTable(tableId);
			
			//and finally get the specific column
			TableColumn column = Database.getDatabase().getTable(tableId).getColumn(columnId);

			//after all is set, we load the columnActionsPanel in the actionsScrollPane
			actionsScrollPane.getViewport().add(new ui.ColumnActionsPanel(column, tableId));
			newColumnButton.setEnabled(true);
		}
		else if(lastSelection.getLevel() == 1) //table
		{
			//we get the table id
			int tableId = getSelectedTableId();

			//get the specific table from the database
			Database database = Database.getDatabase();
			DatabaseTable table = database.getTable(tableId);

			//highlight the table
			_drawPanel.highlightTable(tableId);
			
			//after all is set, we load the tableActionsPanel in the actionsScrollPane
			actionsScrollPane.getViewport().add(new ui.TableActionsPanel(table));
			newColumnButton.setEnabled(true);
		}
		else if(lastSelection.getLevel() == 0) // database
		{
			actionsScrollPane.getViewport().add(new DatabaseActionsPanel());
			newColumnButton.setEnabled(false);
		}
    }//GEN-LAST:event_jTree1ValueChanged

	public JFileChooser getFileChooser()
	{
		return fileChooser;
	}
	
	public boolean fileHasExtension(File file, String extension)
	{
		//fix missign period before the extension
		if(extension.indexOf(".") == -1)
		{
			extension = "."+ extension;
		}
		
		if(file.getName().endsWith(extension))
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	/**
	 * Retrieves a previous database and updates the UI accordingly
	 * 
	 * @param version The version to be retrieved
	 */
	private void displayDatabase(BigDecimal version)
	{
		try
		{
			Database.retrieveSnapShot(version);
		}
		catch(Exception ex)
		{
			if(version.compareTo(Database.getCurrentWorkingInstance().getCurrentVersion()) == 0)
			{
				Database.restoreWorkingInstance();
			}
			else
			{
				log.error(ex.getMessage());
				JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

		refreshDbTree();
		refreshDrawPanel();

		//refreshItems();
	}

	public void updateSqlTextArea(String sqlScript)
	{
		this.sqlTextArea.setText(sqlScript);
	}

	/**
	 * Should be called when a change is made in the database that should affect the UI
	 */
	public final void refreshItems()
	{
		log.trace("Refreshing items...");

		//refresh the version of the database being displayed
		refreshDisplayedVersion();

		//refresh the dbTree
		refreshDbTree();

		//refresh the title
		refreshTitle();

		//refresh the versions textFields
		refreshVersions();
		
		//refresh the tableList
		refreshActionsScrollPane();

		//refresh the versionsCombobox
		refreshVersionsComboBox();

		//refresh the changesTextArea
		refreshChangesTextArea();

		//refresh DrawPanel
		refreshDrawPanel();
	}

	/**
	 * Refreshes the version of the database being displayed
	 */
	private void refreshDisplayedVersion()
	{
		this._displayedDatabaseVersion = Database.getDatabase().getCurrentVersion();
	}

	/**
	 * Refreshes the dbTree
	 */
	private void refreshDbTree()
	{
		jTree1.setModel(new DefaultTreeModel(ui.helpers.DatabaseTree.buildTreeNode()));
	}

	/**
	 * Refreshes the title
	 */
	private void refreshTitle()
	{
		setTitle(Database.getDatabase().getDatabaseName() + " - "+ DbVersioning.getDefaultTitle());
	}

	/**
	 * Refreshes the versions TextFields
	 */
	private void refreshVersions()
	{
		versionTextField.setText(Database.getDatabase().getCurrentVersion().toString());
	}

	/**
	 * Refreshes which actionPane displayed at the time
	 */
	private void refreshActionsScrollPane()
	{
		//take the tree to see what was selected
		DefaultMutableTreeNode lastSelection = (DefaultMutableTreeNode) this.jTree1.getLastSelectedPathComponent();

		if(lastSelection != null)
		{
			if(lastSelection.getLevel() == 0) // the database was selected
			{
				actionsScrollPane.getViewport().add(new DatabaseActionsPanel());
			}
			else if(lastSelection.getLevel() == 1) // table was selected
			{
				//get the id of the table
				int tableId = getSelectedTableId();

				actionsScrollPane.getViewport().add(new TableActionsPanel(Database.getDatabase().getTable(tableId)));
			}
			else if(lastSelection.getLevel() == 2)
			{
				//get the table id
				TreeNode tableNode = lastSelection.getParent();
				int tableId = getSelectedTableId();

				//get the column id
				int columnId = tableNode.getIndex(lastSelection);
				
				//and finally get the specific column
				TableColumn column = Database.getDatabase().getTable(tableId).getColumn(columnId);

				actionsScrollPane.getViewport().add(new ui.ColumnActionsPanel(column, tableId));	
			}
		}
	}

	/**
	 * Refreshes the versionsComboBox with the new changes
	 */
	private void refreshVersionsComboBox()
	{
		LinkedHashMap<String, Integer> hashMap = ChangesLogger.getChangesLogger().getChangesPerVersions();
		Iterator it = hashMap.entrySet().iterator();
		versionsComboBox.removeAllItems();

		while(it.hasNext())
		{
			Map.Entry pairs = (Map.Entry) it.next();

			String comboBoxItem = pairs.getKey() +" -- ("+ pairs.getValue() +" changes)";
			log.trace("Item to add to comboBox: "+ comboBoxItem);

			versionsComboBox.addItem(comboBoxItem);
		}
	}

	/**
	 * Refreshes the ChangesTab with the new changes
	 */
	private void refreshChangesTextArea()
	{
		changesTextArea.setText("");
		for(Change change : ChangesLogger.getChangesLogger().getChanges())
		{
			changesTextArea.setText(changesTextArea.getText() + "\n" + change.getChangeSummary());
		}
	}


	/**
	 * Refreshes the DrawPanel containing the tables
	 */
	private void refreshDrawPanel()
	{
		this._drawPanel.refresh();
	}


	/**
	 * Returns the id of the table which is selected in the tree diagram
	 * If a column is selected, then it returns the id of the table containing
	 * this column
	 * In any other case -1 is returned
	 * 
	 * @return The id of the table or -1 if neither a table nor a column is selected
	 */
	public int getSelectedTableId()
	{
		DefaultMutableTreeNode lastSelection = (DefaultMutableTreeNode) this.jTree1.getLastSelectedPathComponent();

		if(lastSelection != null)
		{
			if(lastSelection.getLevel() == 1)
			{
				return lastSelection.getParent().getIndex(lastSelection);
			}
			else if(lastSelection.getLevel() == 2)
			{
				TreeNode tableNode = lastSelection.getParent();
				return tableNode.getParent().getIndex(tableNode);	
			}
		}

		return -1;
	}
	
	public BigDecimal getDisplayedVersion()
	{
		return this._displayedDatabaseVersion;
	}

	/**
	 * @param args the command line arguments
	 *

	/*
	public static void main(String args[]) {
		/* Set the Nimbus look and feel *
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 *
		/*try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form *
/*		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ApplicationWindow().setVisible(true);
			}
		});
	}*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JScrollPane actionsScrollPane;
    private javax.swing.JTextArea changesTextArea;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTree1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton newColumnButton;
    private javax.swing.JMenuItem newMenu;
    private javax.swing.JButton newTableButton;
    private javax.swing.JButton openButton;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JButton saveButton;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JButton sqlButton;
    private javax.swing.JTextArea sqlTextArea;
    private javax.swing.JTextField versionTextField;
    private javax.swing.JComboBox versionsComboBox;
    // End of variables declaration//GEN-END:variables

	private DrawPanel _drawPanel = new DrawPanel();

	private String _fileName;

	private String _path;

	private final String DEFAULT_NEW_DB_NAME = "newDb";

	private BigDecimal _displayedDatabaseVersion;

	private static ApplicationWindow _instance = null;
	
	/**
	 * The logger instance
	 */
	private static Logger log = Logger.getLogger(ApplicationWindow.class);
}
